var config = iClass.config;
var UserCon = iClass.get("User");
var CommentCon = iClass.get("Comment");
var TagCon = iClass.get("Tag");
iClass.method("Blog", {
	setInfo: function(info) {

		info.title = String(info.title || "").trim();
		info.content = String(info.content || "").trim();
		info.short_content = String(info.short_content || "").trim();
		//hidden_key可以是一个对象，用于描述可浏览这篇博文的人群
		info.hidden_key = $$.boolean_parse(info.hidden_key) && info.hidden_key;
		//标签
		info.tags = Array.isArray(info.tags) ? info.tags : [];
		//类别
		info.type = String(info.type || "blog");
		switch (info.type) {
			case "blog":
				/*
				 * 博文标题
				 */
				if (!info.title) {
					Throw("博文标题不可为空");
				}
				if (info.title.length > config.blogTitleMaxLength) {
					Throw("博文标题长度超过限制（" + config.blogTitleMaxLength + "字以内）");
				}
				this.title = info.title;
				/*
				 * 博文内容
				 */
				if (!info.content) {
					Throw("博文内容不可为空");
				}
				if (info.content.length > config.blogContentMaxLength) {
					Throw("博文内容长度超过限制（" + config.blogContentMaxLength + "字以内）");
				}
				this.content = info.content;
				break;
			case "short":
				/*
				 * 动态
				 */
				if (!info.short_content) {
					Throw("动态内容不可为空");
				}
				if (info.short_content.length > config.blogTitleMaxLength) {
					Throw("动态内容长度超过限制（" + config.blogTitleMaxLength + "字以内）");
				}
				this.short_content = info.short_content;
		}
		this.type = info.type;
		/*
		 * 博文私密性
		 * [{type,value}]:
		 *   ["user_id",user_id](指定用户)
		 *   ["key",key](钥匙)
		 *   ["group",group_name](用户分组)
		 */
		this.hidden_key = info.hidden_key;

		/*
		 * 标签
		 */
		this.tags = info.tags.map(function(tag_info) {
			if (typeof tag_info === "string") {
				var tag_id = tag_info
			} else {
				tag_id = tag_info._id
			};
			if (!tag_id) {
				Throw("标签格式错误")
			}
			if (!TagCon.get(tag_id)) {
				Throw("找不到指定标签")
			}
			return tag_id
		});

		this.last_update_time = +new Date;
		this.save();
	},
	//获取博文详情
	getDetail: function(visitor_id) {
		var result = this.clone();

		//作者信息
		result.user = UserCon.get(this.user_id);

		//评论集合
		result.comments = this.getComments(visitor_id);

		result.tags = (Array.isArray(result.tags) ? result.tags : []).map(function(tag_id) {
			return TagCon.get(tag_id)
		}).clearNull();

		if (this.tags.length !== result.tags.length) { //有残留数据，清除残留数据
			this.tags = result.tags.map(function(tag) {
				return tag._id
			});
			this.save();
		}

		return result;
	},
	//判断该博文是否对这个用户开放，或者根据钥匙信息进行开放
	isOpenForSomeUser: function(user_id, key_info) {
		//无设置密匙，开放
		if (!this.hidden_key) {
			return true;
		}
		//自己浏览，开放
		if (user_id === this._id) {
			return true;
		}
		//其它情况
		return false;
	},
	getComments: function(visitor_id) {
		var comments = db.find_list(CommentCon.dbname, {
			parent_id: "",
			blog_id: this._id
		}).reverse().map(function(comment) {
			return CommentCon.getInstance(comment).getDetail(visitor_id);
		});
		return comments;
	},
	getHostComments: function(visitor_id) {
		var comments = db.find_list(CommentCon.dbname, {
			parent_id: "",
			blog_id: this._id
		}).reverse().map(function(comment) {
			return CommentCon.getInstance(comment).getDetail(visitor_id);
		}).sort(function(comment_a, comment_b) {
			return comment_a.like_num.length < comment_b.like_num.length;
		});
		return comments;
	},
	//添加观点
	_addViewpoint: function(user_id, type) {
		var viewpoint = Array.isArray(this.viewpoint) ? this.viewpoint : (this.viewpoint = []);
		if (viewpoint.filter(function(info) {
				return info.user_id == user_id;
			}).length > 0) {
			Throw("用户已经表过态");
		}
		viewpoint.push({
			user_id: user_id,
			type: type
		});
		this.save();
	}
});